package com.stylefeng.guns.rest.modular.wz.service.impl;

import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.modular.wz.service.IWzPrizeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 奖品表 服务实现类
 * </p>
 *
 * @author ruiyifeng123
 * @since 2018-06-12
 */
@Service
public class WzPrizeServiceImpl extends ServiceImpl<WzPrizeMapper, WzPrize> implements IWzPrizeService {

    @Autowired
    private WzUserFinanceRecordMapper userFinanceRecordMapper;
    @Autowired
    private WzPrizeWinnersMapper prizeWinnersMapper;
    @Autowired
    private WzPrizeMapper prizeMapper;
    @Autowired
    private WzUserCouponMapper userCouponMapper;
    @Autowired
    private WzUserMapper userMapper;

    /**
     * 抽中奖数据处理
     */
    @Transactional
    @Override
    public boolean savePrizeWinner(WzUserFinanceRecord userFinanceRecord, WzPrizeWinners prizeWinners, WzUserCoupon userCoupon, WzPrize prize, WzUser user) {
        int r = r = userMapper.updateById(user);
        if(r > 0){
            r = prizeMapper.updateById(prize);
            if(r  >0){
                r = userCouponMapper.insert(userCoupon);
                if(r  >0){
                    r = prizeWinnersMapper.insert(prizeWinners);
                    if(r  >0){
                        userFinanceRecordMapper.insert(userFinanceRecord);
                        if(r  >0){
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    /**
     * 抽奖失败数据处理
     */
    @Transactional
    public boolean savePrizeFail(WzPrize prize,WzUser user, WzUserFinanceRecord userFinanceRecord) {
        int r = userMapper.updateById(user);
        if(r > 0){
            r = prizeMapper.updateById(prize);
            if(r > 0){
                r =  userFinanceRecordMapper.insert(userFinanceRecord);
                if(r > 0){
                    return true;
                }
            }
        }
        return false;
    }
}
